package cn.ac.jaas.isc.vegetable.pbms.service.impl;

import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcBase;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcGroupMember;
import cn.ac.jaas.isc.vegetable.pbms.api.vo.GroupIdAndDeviceIdVO;
import cn.ac.jaas.isc.vegetable.pbms.api.vo.GroupMemberVO;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcGroupMemberMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcGroupModel;
import cn.ac.jaas.isc.vegetable.pbms.mapper.ArcGroupModelMapper;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcGroupModelService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张高昌
 * @date 2022-05-28 17:25:08
 */
@Service
@AllArgsConstructor
public class ArcGroupModelServiceImpl extends ServiceImpl<ArcGroupModelMapper, ArcGroupModel> implements ArcGroupModelService {

    private final ArcGroupMemberMapper arcGroupMemberMapper;

    @Override
    public R saveGroupModel(ArcGroupModel arcGroupModel) {
        return R.ok(true);
    }

    @Override
    public R updateGroupModel(ArcGroupModel arcGroupModel) {
        return R.ok(true);
    }

    @Override
    public Page getGroupPage(Page page, Integer houseId, Integer groupId) {
        List<GroupMemberVO> pageList = baseMapper.selectGroupMemberPage(page, houseId, groupId);
        for (GroupMemberVO groupMemberVO : pageList) {
            LambdaQueryWrapper<ArcGroupMember> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(ArcGroupMember::getDeviceId, groupMemberVO.getDeviceId())
                    .ne(ArcGroupMember::getGroupId, groupMemberVO.getGroupId());
            List<ArcGroupMember> arcGroupMembers = arcGroupMemberMapper.selectList(wrapper);
            if (arcGroupMembers.size() > 0) {
                groupMemberVO.setOtherGroup("是");
            } else {
                groupMemberVO.setOtherGroup("否");
            }
        }
        return page.setRecords(pageList);
    }

    @Override
    public boolean removeDeviceById(Integer modelId) {
        int result = arcGroupMemberMapper.deleteById(modelId);
        return result > 0;
    }

    @Override
    public boolean bindingDevice(GroupIdAndDeviceIdVO vo) {

        Integer[] deviceIds  = vo.getDeviceIds();
        for (Integer deviceId : deviceIds) {
            LambdaQueryWrapper<ArcGroupMember> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(ArcGroupMember::getDeviceId, deviceId)
                    .eq(ArcGroupMember::getGroupId, vo.getGroupId());
            int integer = arcGroupMemberMapper.selectCount(wrapper);
            //如果已经存在的就不需要在添加了
            if (integer != 0) {
                continue;
            }
            ArcGroupMember arcGroupMember = new ArcGroupMember();
            arcGroupMember.setGroupId(vo.getGroupId());
            arcGroupMember.setDeviceId(deviceId);
            arcGroupMemberMapper.insert(arcGroupMember);
        }
        return true;
    }
}
